﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MigratorCore;
using MigratorCore.Bases;
using MigratorCore.Actions;

namespace MigratorWizard
{
    public partial class ProviderList : UserControl
    {
        private Main main;

        public Main Main
        {
            set { main = value; main.NextClick += new NextClick(main_NextClick); }
        }

        void main_NextClick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                foreach (IProvider p in main.Migrator.Providers)
                {
                    if (p.Name == sourceProviders.SelectedItems[0].Text)
                        main.Migrator.Source = p;
                    if (p.Name == destinationProviders.SelectedItems[0].Text)
                        main.Migrator.Destination = p;
                }
            }
        }

        public ProviderList()
        {
            InitializeComponent();
        }

        private void ProviderList_Load(object sender, EventArgs e)
        {
            destinationProviders.Items.Clear();
            sourceProviders.Items.Clear();
            if (main != null)
            foreach (IProvider provider in main.Migrator.Providers)
            {
                bool s = false;
                bool d = false;
                List<string> ss = new List<string>();
                List<string> dd = new List<string>();
                foreach (IAction action in provider.Actions)
                {
                    if (action.GetType().GetInterfaces()[0] == typeof(IDestinationAction))
                    {
                        d = true;
                        dd.Add(action.Name);
                    }
                    else if (action.GetType().GetInterfaces()[0] == typeof(ISourceAction))
                    {
                        s = true;
                        ss.Add(action.Name);
                    }
                }
                if (s)
                {
                    ListViewItem i = new ListViewItem(provider.Name);
                    i.SubItems.Add(string.Join(", ", ss.ToArray()));
                    i.Group = sourceProviders.Groups[0];
                    sourceProviders.Items.Add(i);
                }
                if (d)
                {
                    ListViewItem i = new ListViewItem(provider.Name);
                    i.SubItems.Add(string.Join(", ", dd.ToArray()));
                    i.Group = destinationProviders.Groups[0];
                    destinationProviders.Items.Add(i);
                }
            }
        }


        private void Providers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (destinationProviders.SelectedItems.Count > 0 && sourceProviders.SelectedItems.Count > 0)
                main.EnableNext = true;
            else main.EnableNext = false;
        }

        private void ProviderList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible && (main != null))
            {
                this.main.EnableNext = false;
                this.main.EnableBack = true;
                this.main.Text = "The Migrator Project - Select Providers";
            }
        }

        private void sourceProviders_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if (sourceProviders.SelectedIndices.Count > 0)
                {
                    SettingsDialog sd = new SettingsDialog(main.Migrator.Providers[0].Settings, main.Migrator.Providers[0].Name);
                    sd.ShowDialog(this);
                }
        }

        private void destinationProviders_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if (destinationProviders.SelectedIndices.Count > 0)
                {
                    SettingsDialog sd = new SettingsDialog(main.Migrator.Providers[0].Settings, main.Migrator.Providers[0].Name);
                    sd.ShowDialog(this);
                }
        }

    }
}
